package com.zj.leetcode.easy.greed;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zhangbo
 * @program: algorithm
 * @description: 409. 最长回文串
 * @date 2022-03-20 10:48:19
 */
public class LongestPalindrome {
    public static void main(String[] args) {
        Solution solution = new LongestPalindrome().new Solution();
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int longestPalindrome(String s) {

            HashMap<Character, Integer> map = new HashMap<>();
            for (int i = 0; i < s.length(); i++) {
                map.put(s.charAt(i), map.getOrDefault(s.charAt(i), 0) + 1);
            }

            int count = 0;
            boolean flag = false;
            for (Map.Entry<Character, Integer> entry : map.entrySet()) {
                if ((entry.getValue() % 2 == 0)) {
                    count = count + entry.getValue();
                } else {
                    flag = true;
                    if(entry.getValue() > 1){
                        count = count + (entry.getValue() - 1);
                    }
                }
            }
            return flag ? count + 1 : count;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}
